<?php
session_start();
numericVerification();

//数字验证码
function numericVerification($width = 100, $heigth = 30, $point = 200, $line = 3)
{
    $image   = imagecreatetruecolor($width, $heigth);
    $bgcolor = imagecolorallocate($image, 255, 255, 255);
    imagefill($image, 0, 0, $bgcolor);
    $code = '';
    for ($i = 0; $i < 4; $i++) {
        $fontsize    = 10;
        $fontcolor   = imagecolorallocate($image, rand(0, 120), rand(0, 120), rand(0, 120));
        $fontcontent = rand(0, 9);
        $code        .= $fontcontent;
        $x           = ($i * $width / 4) + rand($heigth / 5, $heigth / 5 * 2);
        $y           = rand($heigth / 5, $heigth / 5 * 2);
        imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
    }
    $_SESSION['numeric_code'] = $code;
    //增加点
    for ($i = 0; $i < $point; $i++) {
        $pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));
        imagesetpixel($image, rand(1, $width - 1), rand(1, $heigth - 1), $pointcolor);
    }
    //增加线
    for ($i = 0; $i < $line; $i++) {
        $linecolor = imagecolorallocate($image, rand(80, 220), rand(80, 220), rand(80, 220));
        imageline($image, rand(1, $width - 1), rand(1, $heigth - 1), rand(1, $width - 1), rand(1, $heigth - 1), $linecolor);
    }
    header('content-type: image/png');
    imagepng($image);
    imagedestroy($image);
}

